package com.parktech.sun.web.controller.management.sunhz;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.sunhz.Enterprise;
import com.parktech.sun.domain.model.sunhz.EnterpriseCategoryProduct;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.sunhz.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * User: Think
 * Date: 11-1-15
 * Time: 下午9:08
 */
@Controller
@RequestMapping("/management/enterprise/products")
public class EnterpriseProductionController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private EnterpriseService enterpriseService;

    @Autowired
    public EnterpriseProductionController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(@ModelAttribute EnterpriseSearchForm enterpriseSearchForm) {

        Page page = new Page(pageSize, enterpriseSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/sunhz/enterpriseProductSearch");

        return modelAndView.addObject(
                enterpriseService.searchCategoryProduct(
                        enterpriseSearchForm.getName(), page));
    }

    @ModelAttribute("allCategories")
    public Map<Long, String> populateAllCategories() {
        Map<Long, String> allCategories = new TreeMap<Long, String>();
        allCategories.put(1L, "硅料与硅片");
        allCategories.put(2L, "电池片与组件");
        allCategories.put(4L, "薄膜电池");
        allCategories.put(8L, "辅料与设备");
        allCategories.put(16L, "系统集成");
        allCategories.put(32L, "服务机构");
        return allCategories;
    }

    @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
    public String edit(@PathVariable("productId") Long productId, Model model) {
        EnterpriseCategoryProduct product = enterpriseService.getProductById(productId);
        model.addAttribute(product);
        return "management/sunhz/enterpriseProductForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new EnterpriseCategoryProduct());
        return "management/sunhz/enterpriseProductForm";
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("enterpriseProductIds") Long[] enterpriseIds) {
        enterpriseService.deleteAll(EnterpriseCategoryProduct.class, enterpriseIds);
        return "redirect:search";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@ModelAttribute @Valid EnterpriseCategoryProduct enterpriseCategoryProduct, BindingResult result) throws IOException {

        if (result.hasErrors()) {
            return "management/sunhz/enterpriseProductForm";
        }

        enterpriseService.saveOrUpdateCategoryProduct(enterpriseCategoryProduct);

        return "redirect:/management/enterprise/products/search";
    }

}
